// by Xeno
#define THIS_FILE "fn_spawn_farp.sqf"
#include "x_setup.sqf"
private ["_nos", "_notruck", "_vt", "_helper1", "_helper2", "_helper3", "_helper4", "_mt", "_helper", "_farptype", "_farp", "_exitit"];

if (__pGetVar(GVAR(isinaction))) exitWith {
	GVAR(commandingMenuIniting) = false;
};

if (player distance GVAR(name_flag_base) < 30) exitWith {
	systemChat (localize "STR_DOM_MISSIONSTRING_246");
	GVAR(commandingMenuIniting) = false;
};

if ((player call FUNC(GetHeight)) > 5) exitWith {
	systemChat (localize "STR_DOM_MISSIONSTRING_241");
	GVAR(commandingMenuIniting) = false;
};

if (count __pGetVar(GVAR(farp_pos)) > 0) exitWith {
	systemChat (localize "STR_DOM_MISSIONSTRING_242");
	GVAR(commandingMenuIniting) = false;
};

_nos = (position player) nearEntities ["Truck", 20];
_notruck = true;
if (count _nos > 0) then {
	{
		_vt = GV(_x,GVAR(vec_type));
		if (!isNil "_vt" && {_vt == "Engineer"}) exitWith {_notruck = false};
	} forEach _nos;
};

if (_notruck) exitWith {
	systemChat (localize "STR_DOM_MISSIONSTRING_243");
	GVAR(commandingMenuIniting) = false;
};

_d_farp_pos = player modeltoworld [0,8,0];
_d_farp_pos set [2,0];

if (surfaceIsWater [_d_farp_pos select 0, _d_farp_pos select 1]) exitWith {
	systemChat (localize "STR_DOM_MISSIONSTRING_244");
	GVAR(commandingMenuIniting) = false;
};

if (GVAR(with_ranked) && {score player < (GVAR(ranked_a) select 20)}) exitWith {
	(format [(localize "STR_DOM_MISSIONSTRING_245"), score player, GVAR(ranked_a) select 20]) call FUNC(HQChat);
	GVAR(commandingMenuIniting) = false;
};

_helper1 = GVAR(HeliHEmpty) createVehicleLocal [_d_farp_pos select 0, (_d_farp_pos select 1) + 4, 0];
_helper2 = GVAR(HeliHEmpty) createVehicleLocal [_d_farp_pos select 0, (_d_farp_pos select 1) - 4, 0];
_helper3 = GVAR(HeliHEmpty) createVehicleLocal [(_d_farp_pos select 0) + 4, _d_farp_pos select 1, 0];
_helper4 = GVAR(HeliHEmpty) createVehicleLocal [(_d_farp_pos select 0) - 4, _d_farp_pos select 1, 0];

_exit_it = false;
if ((abs (((getPosASL _helper1) select 2) - ((getPosASL _helper2) select 2)) > 2) || {(abs (((getPosASL _helper3) select 2) - ((getPosASL _helper4) select 2)) > 2)}) then {
	systemChat (localize "STR_DOM_MISSIONSTRING_246");
	_exit_it = true;
};

for "_mt" from 1 to 4 do {call compile format ["deleteVehicle _helper%1;", _mt]};

if (_exit_it) exitWith {
	GVAR(commandingMenuIniting) = false;
};

__pSetVar [QGVAR(isinaction), true];

if (GVAR(with_ranked)) then {[QGVAR(pas), [player, (GVAR(ranked_a) select 20) * -1]] call FUNC(NetCallEventCTS)};

player playMove "AinvPknlMstpSlayWrflDnon_medic";
sleep 3;
waitUntil {animationState player != "AinvPknlMstpSlayWrflDnon_medic"};
GVAR(commandingMenuIniting) = false;
if (!alive player) exitWith {
	systemChat (localize "STR_DOM_MISSIONSTRING_247");
	__pSetVar [QGVAR(isinaction), false];
};

_dir_to_set = getdir player;

_farptype = switch (GVAR(player_side)) do {
	case blufor: {"US_WarfareBVehicleServicePoint_Base_EP1"};
	case opfor: {"TK_WarfareBVehicleServicePoint_Base_EP1"};
};

_farp = createVehicle [_farptype, _d_farp_pos, [], 0, "NONE"];
_farp setdir _dir_to_set;
_farp setPos _d_farp_pos;
[_farp, 0] call FUNC(SetHeight);
player reveal _farp;
_d_farp_pos = position _farp;
__pSetVar [QGVAR(farp_pos), _d_farp_pos];

__pSetVar [QGVAR(farp_obj), _farp];

_farpsar = __XJIPGetVar(GVAR(farps));
_farpsar set [count _farpsar, _farp];
[QGVAR(farps), _farpsar] call FUNC(NetSetJIP);

systemChat (localize "STR_DOM_MISSIONSTRING_248");
_m_name = "FARP " + GVAR(string_player);
[QGVAR(p_o_a), [GVAR(string_player), [_farp,_m_name,GVAR(name_pl),GVAR(player_side)]]] call FUNC(NetCallEvent);

_farp addAction [(localize "STR_DOM_MISSIONSTRING_249") call FUNC(RedText), {
	private "_farp";
	_farp = __pGetVar(GVAR(farp_obj));
	if (isNil "_farp") exitWith {};

	player playMove "AinvPknlMstpSlayWrflDnon_medic";
	sleep 3;
	waitUntil {animationState player != "AinvPknlMstpSlayWrflDnon_medic"};
	if (!alive player) exitWith {systemChat (localize "STR_DOM_MISSIONSTRING_315")};

	_farpsar = __XJIPGetVar(GVAR(farps)); 
	_farpsar = _farpsar - [_farp];
	[QGVAR(farps), _farpsar] call FUNC(NetSetJIP);

	deleteVehicle _farp;
	__pSetVar [QGVAR(farp_obj), objNull];

	systemChat (localize "STR_DOM_MISSIONSTRING_316");
	__pSetVar [QGVAR(farp_pos), []];
	_m_name = "FARP " + GVAR(string_player);
	[QGVAR(p_o_r), [GVAR(string_player),_m_name]] call FUNC(NetCallEvent);
}];

[QGVAR(farp_e), _farp] call FUNC(NetCallEventToClients);

__pSetVar [QGVAR(isinaction), false];
